package com.iplant.basic.controller.fmc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.iplant.basic.controller.BasicController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.FMCService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.fmc.FMCStation;

@RestController
@RequestMapping("/api/FMCStation")
public class FMCStationController extends BasicController<FMCStation> {
    private static Logger logger = LoggerFactory.getLogger(FMCStationController.class);

    @Autowired
    FMCService wFMCService;

    public FMCStationController() {
        super(FMCStation.class);
    }

    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"));
            int wWorkshopID = StringUtils.parseInt(request.getParameter("WorkshopID"));
            int wWorkAreaID = StringUtils.parseInt(request.getParameter("WorkAreaID"));
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"));
            List<Integer> wLineID = StringUtils.parseIntList(request.getParameter("LineID"), ",");
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            int wHasResource = StringUtils.parseInt(request.getParameter("HasResource"));

            Pagination wPagination = GetPagination(request);
            ServiceResult<List<FMCStation>> wServiceResult = wFMCService.FMC_QueryStationList(wLoginUser, wFactoryID,
                    wWorkshopID, wLineID, wWorkAreaID, wDeviceID, wName, wActive, wHasResource, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            int wHasResource = StringUtils.parseInt(request.getParameter("HasResource"));

            ServiceResult<FMCStation> wServiceResult = wFMCService.FMC_QueryStation(wLoginUser, wID, wCode,
                    wHasResource);
            FMCStation wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/ip")
    public Object getClientIp(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String ipAddress = request.getHeader("X-Forwarded-For");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
            }

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, ipAddress);
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
